import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Logger {
    private static final Logger instance = new Logger();
    private final ConcurrentLinkedQueue<String> logQueue = new ConcurrentLinkedQueue<>();
    private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");

    private Logger() {}

    public static Logger getInstance() {
        return instance;
    }

    public void log(String message) {
        String timestamp = sdf.format(new Date());
        logQueue.add(timestamp + " - " + message);
    }

    public void saveToFile(String filename) throws IOException {
        try (PrintWriter pw = new PrintWriter(new FileWriter(filename))) {
            while (!logQueue.isEmpty()) {
                pw.println(logQueue.poll());
            }
        }
    }
}
